package com.cr.admin.business.city.controller;


import com.cr.admin.business.city.pojo.entity.City;
import com.cr.admin.business.city.pojo.param.CityAddParam;
import com.cr.admin.business.city.pojo.param.CityUpdateParam;
import com.cr.admin.business.city.pojo.query.CityQuery;
import com.cr.admin.business.city.service.CityService;
import com.cr.common.response.JsonResult;
import com.cr.common.response.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController

@Api(tags = "城市管理")
@RequestMapping("/api/bus/city")
public class CityController {
    @Autowired
    private CityService cityService;

    @ApiOperation(value = "查询城市列表")
    @GetMapping
    public JsonResult list(CityQuery cityQuery ) {
        PageData<City> pageData = PageData.page(cityQuery.getPageNum(), cityQuery.getPageSize(), () -> cityService.list(cityQuery));
        return JsonResult.success(pageData);
    }

    @ApiOperation(value = "新增城市")
    @PostMapping
    public JsonResult insert(@Validated @RequestBody CityAddParam cityAddParam) {
        cityService.insert(cityAddParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "修改城市")
    @PutMapping()
    public JsonResult update(@Validated @RequestBody CityUpdateParam topicUpdateParam) {
        cityService.updateById(topicUpdateParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询城市详情")
    @GetMapping("{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        City city = cityService.selectById(id);
        return JsonResult.success(city);
    }


    @ApiOperation(value = "删除城市")
    @DeleteMapping()
    public JsonResult delete(@RequestParam(name = "ids") List<Long> ids){
        cityService.deleteByIds(ids);
        return JsonResult.success();
    }


}
